import pymysql.cursors
class database:
    def __init__(self):
        self.conn = pymysql.connect(host='125.222.144.16',
                                     port=3306,
                                     user='root',
                                     password='xgxxgx',
                                     db='avmoo',
                                     charset='utf8mb4',
                                     cursorclass=pymysql.cursors.DictCursor)
    def getavstar(self,avstarname):
        connection=self.conn
        try:
            connection.open
            with connection.cursor() as cursor:
                # 执行sql语句，插入记录
                sql = "select * from avstar where JapaneseName=%s order by id"
                cursor.execute(sql, (avstarname));
            # 没有设置默认自动提交，需要主动提交，以保存所执行的语句
                connection.commit()
                result = cursor.fetchone()
                print(result)
                return result
        except Exception as err:
            print(err)
            return  None
    def getavmovie(self,avcode):
        connection = self.conn
        try:
            connection.open
            with connection.cursor() as cursor:
                # 执行sql语句，插入记录
                sql = "select * from avmovie where Code like '%s' order by id"
                print(sql % (avcode))
                cursor.execute(sql % (avcode))
                # 没有设置默认自动提交，需要主动提交，以保存所执行的语句
                connection.commit()
                result = cursor.fetchone()
                print(result)
                return result
        except Exception as err:
            print(err)
            return None
    def insertavstar(self,avstarname):
        connection = self.conn
        try:
            connection.open
            with connection.cursor() as cursor:
                # 执行sql语句，插入记录
                sql = "insert into avstar (JapaneseName) values (%s)"
                cursor.execute(sql, (avstarname));
                # 没有设置默认自动提交，需要主动提交，以保存所执行的语句
                connection.commit()

                return True
        except:
            return  False
    def closedb(self):
        connection = self.conn
        connection.close()

    def insertavmovie(self, avinfo):
        connection = self.conn
        try:
            connection.open
            with connection.cursor() as cursor:
                # 执行sql语句，插入记录
                sql = "insert into avmovie (AvBigLogoURL,MovieDetailUrl,Code,PublisDateTime,AvstarID,AvstarName,LogoURL,Publisher,Maker,MagnetUrl) values ('%s','%s','%s','%s',%d,'%s','%s','%s','%s','%s')"
                print (sql % (
                avinfo.BigImage, avinfo.MovieDetailUrl, avinfo.Code, avinfo.PublishDatetime, int(avinfo.AvStarID),
                avinfo.StarName,avinfo.LogoURL,avinfo.Publisher, avinfo.Maker, avinfo.MagUrl))
                cursor.execute(sql % (
                avinfo.BigImage, avinfo.MovieDetailUrl, avinfo.Code, avinfo.PublishDatetime, int(avinfo.AvStarID),
                avinfo.StarName,avinfo.LogoURL,avinfo.Publisher, avinfo.Maker, avinfo.MagUrl));
                # 没有设置默认自动提交，需要主动提交，以保存所执行的语句
                connection.commit()

                return True
        except Exception as err:
            print(err)
            return False
